import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/model/market_model/market_batch_sale_list_model.dart';
import 'package:yysk_app/pages/nft_pages/market_pages/market_collection_buy/market_collection_buy_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'package:yysk_app/widget/refresh_and_scroll_widget.dart';
import 'package:oktoast/oktoast.dart';

// class BulkSalesAndCancellationOfSalesPage extends {}
class MarketCollectionBuyPage extends GetView<MarketCollectionBuyController>
    implements PreferredSizeWidget {
  static const String path = '/MarketCollectionBuyPage';
  const MarketCollectionBuyPage({super.key});
  @override
  Size get preferredSize => const Size.fromHeight(kToolbarHeight);
  @override
  Widget build(BuildContext context) {
    double top = MediaQuery.of(context).padding.top;
    return Scaffold(
      bottomNavigationBar: Obx(() {
        return controller.isBatch.value == false
            ? _bottomBuy(context: context)
            : _bottomBuyBatch(context: context);
      }),
      body:     Scaffold(
          body: Stack(
            children: [
              Padding(
                padding: EdgeInsets.fromLTRB(0.px, 0, 0.px, 0.px),
                child: Obx(
                  () => Stack(
                    children: [
                          Positioned(
            top: 0,
            left: 0,
            right: 0,
            child: ImageAssetWidget(
              width: 375.px,
              height: 812.px,
              imageUrl: 'static/images/bg.png',
            ),
          ),
                         Column(
                    children: [
                      SizedBox(height: top,),
                      CustomAppBar2(title: '购买藏品', context: context),

                      _unSaleHead(context: context),
                      Text(
                        '${controller.count}',
                        style: TextStyle(fontSize: 0),
                      ),
                      Expanded(
                        child: Padding(
                          padding: EdgeInsets.only(left: 18.px, right: 18.px),
                          child: RefreshAndScrollWidget(
                            onRefresh: controller.handleRefresh,
                            scrollController: controller.scrollController,
                            itemBuilder: (item, index) => _listItemUnSale(item),
                            itemCount: controller.saleList.length,
                            items: controller.saleList,
                          ),
                        ),
                      ),
                    ],
                  ),
                
                    ],
                  )
               ),
              ),
            ],
          ),
        ),
     
    );
  }

  _listItemUnSale(MarketBatchSaleListModelItem item) {
    return Container(
      padding: EdgeInsets.only(top: 20.px, bottom: 20.px),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(width: 1.px, color: Color.fromRGBO(59, 62, 69, 1)),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Container(
            width: 106.px,
            child: Text(
              '#${item.tokenId}',
              style: TextStyle(fontSize: 14.px, color: AppColors.fontColor),
              textAlign: TextAlign.left,

              overflow: TextOverflow.ellipsis,
            ),
          ),
          Container(
            width: 84.px,
            child: Center(
              child: Text(
                '${item.salePrice}',
                style: TextStyle(fontSize: 14.px, color: AppColors.fontColor),
                textAlign: TextAlign.center,

                overflow: TextOverflow.ellipsis,
              ),
            ),
          ),
          Obx(() {
            return GestureDetector(
              onTap: () {
                if (controller.isBatch.value == false &&
                    item.status == 'NORMAL' &&
                    item.isMyself == false) {
                  controller.isSm().then((res) {
                    if (res == true) {
                      controller.createFirstOrder(idOne: "${item.id}");
                    }
                  });
                } else if (controller.isBatch.value == true &&
                    item.status == 'NORMAL' &&
                    item.isMyself == false) {
                  controller.chooseListUnSell(item.id);
                }
              },
              child: SizedBox(
                width: 84.px,
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text('${controller.count}', style: TextStyle(fontSize: 0)),
                    if (item.status == 'NORMAL' &&
                        controller.isBatch.value == false &&
                        item.isMyself == false)
                      Container(
                        width: 44.px,
                        height: 21.px,
                        decoration: BoxDecoration(
                          border: Border.all(
                            width: 1.px,
                            color: AppColors.startColor,
                          ),
                          borderRadius: BorderRadius.circular(11.px),
                        ),
                        child: Center(
                          child: Text(
                            '购',
                            style: TextStyle(
                              color: AppColors.startColor,
                              fontSize: 12.px,
                            ),
                          ),
                        ),
                      ),
                    if (item.status == 'NORMAL' &&
                        controller.isBatch.value == false &&
                        item.isMyself == true)
                      Container(
                        width: 44.px,
                        height: 21.px,
                        decoration: BoxDecoration(
                          // border: Border.all(width: 1.px,color: Color.fromRGBO(255, 66, 82, 0.2)),
                          borderRadius: BorderRadius.circular(11.px),
                          color: Color.fromRGBO(255, 66, 82, 0.2),
                        ),
                        child: Center(
                          child: Text(
                            '我的',
                            style: TextStyle(
                              color: AppColors.fontColorRed2,
                              fontSize: 12.px,
                            ),
                          ),
                        ),
                      ),
                    if (item.status == 'FROZEN')
                      Container(
                        width: 44.px,
                        height: 21.px,
                        decoration: BoxDecoration(
                          // border: Border.all(width: 1.px,color: Color.fromRGBO(255, 66, 82, 0.2)),
                          borderRadius: BorderRadius.circular(11.px),
                          color: Color.fromRGBO(255, 66, 82, 0.2),
                        ),
                        child: Center(
                          child: Text(
                            '锁',
                            style: TextStyle(
                              color: AppColors.fontColorRed2,
                              fontSize: 12.px,
                            ),
                          ),
                        ),
                      ),
                    if (item.status == 'NORMAL' &&
                        controller.isBatch.value == true &&
                        item.isMyself == true)
                      Container(
                        width: 44.px,
                        height: 21.px,
                        decoration: BoxDecoration(
                          // border: Border.all(width: 1.px,color: Color.fromRGBO(255, 66, 82, 0.2)),
                          borderRadius: BorderRadius.circular(11.px),
                          color: Color.fromRGBO(255, 66, 82, 0.2),
                        ),
                        child: Center(
                          child: Text(
                            '我的',
                            style: TextStyle(
                              color: AppColors.fontColorRed2,
                              fontSize: 12.px,
                            ),
                          ),
                        ),
                      ),
                    if (item.status == 'NORMAL' &&
                        item.isSelect == true &&
                        controller.isBatch.value == true &&
                        item.isMyself == false)
                      ImageAssetWidget(
                        imageUrl: 'static/images/mine/choose.png',
                        width: 18.px,
                        height: 18.px,
                      ),
                    if (item.status == 'NORMAL' &&
                        item.isSelect == false &&
                        controller.isBatch.value == true &&
                        item.isMyself == false)
                      ImageAssetWidget(
                        imageUrl: 'static/images/mine/unChoose.png',
                        width: 18.px,
                        height: 18.px,
                      ),
                  ],
                ),
              ),
            );
          }),
        ],
      ),
    );
  }

  _unSaleHead({required BuildContext context}) {
    return Container(
      width: context.width,
      padding: EdgeInsets.fromLTRB(18.px, 12.px, 18.px, 12.px),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.only(
          topLeft: Radius.circular(18.px),
          topRight: Radius.circular(18.px),
        ),
        gradient: LinearGradient(
          colors: [
            Color.fromRGBO(31, 33, 38, 1),
            Color.fromRGBO(30, 33, 38, 0),
          ],
          begin: Alignment.topCenter,
          end: Alignment.bottomCenter,
        ),
        border: Border(
          bottom: BorderSide(
            width: 0.5.px,
            color: AppColors.backgroundColorGrey,
          ),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            '当前在售 (${controller.total.value})',
            style: TextStyle(
              fontSize: 14.px,
              color: AppColors.backgroundColorGrey,
            ),
            textAlign: TextAlign.left,
          ),
          SizedBox(width: 4.px),
          GestureDetector(
            onTap: () {
              controller.totalPrice.value = '0';
              controller.saleListChoose.value = [];

              controller.orderBy.value =
                  controller.orderBy.value == 'asc' ? 'desc' : 'asc';
              controller.getList();
            },
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Center(
                  child: Text(
                    '价格',
                    style: TextStyle(
                      fontSize: 14.px,
                      color: AppColors.backgroundColorGrey,
                    ),
                    textAlign: TextAlign.left,
                  ),
                ),

                controller.orderBy.value == 'asc'
                    ? ImageAssetWidget(
                      imageUrl: 'static/images/asc_bg.png',
                      width: 14.px,
                      height: 17.px,
                      fit: BoxFit.fitWidth,
                    )
                    : ImageAssetWidget(
                      imageUrl: 'static/images/desc_bg.png',
                      width: 14.px,
                      height: 17.px,
                      fit: BoxFit.fitWidth,
                    ),
              ],
            ),
          ),

          SizedBox(width: 10.px),

          Text(
            '可购 (${controller.canBuyCount.value})',
            style: TextStyle(
              fontSize: 14.px,
              color: AppColors.backgroundColorGrey,
            ),
            textAlign: TextAlign.left,
          ),
        ],
      ),
    );
  }

  _bottomBuy({required BuildContext context}) {
    return Container(
      width: context.width,
      height: 60.px,
      margin: EdgeInsets.only(top: 10.px, bottom: 10.px),
      decoration: BoxDecoration(color: Color.fromRGBO(31, 33, 38, 1)),
      padding: EdgeInsets.only(
        left: 16.px,
        right: 14.px,
        top: 10.px,
        bottom: 10.px,
      ),
      child: Obx(() {
        return Center(
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              // Text(
              //   '已选：${controller.num}',
              //   style: TextStyle(
              //     color: AppColors.fontColor,
              //     fontWeight: FontWeight.w500,
              //     fontSize: 14.px,
              //   ),
              // ),
              GestureDetector(
                onTap: () {
                  controller.isBatch.value = true;
                },
                child: Container(
                  width: 340.px,
                  height: 40.px,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(100),
                    gradient: LinearGradient(
                      colors: [
                        AppColors.startColor,
                        AppColors.endColor,
                      ], // 定义渐变颜色
                      begin: Alignment.centerLeft,
                      end: Alignment.centerRight,
                    ),
                  ),
                  child: Center(
                    child: Text(
                      '${controller.title}',
                      style: TextStyle(
                        color: AppColors.backgroundColorBlack,
                        fontWeight: FontWeight.bold,
                        fontSize: 15.px,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        );
      }),
    );
  }

  _bottomBuyBatch({required BuildContext context}) {
    return Container(
      width: context.width,
      height: 110.px,
      decoration: BoxDecoration(
        color: Color.fromRGBO(13, 14, 18, 1),
        border: Border(
          top: BorderSide(width: 0.5.px, color: Color.fromRGBO(59, 62, 69, 1)),
        ),
      ),
      child: Padding(
        padding: EdgeInsets.only(
          top: 10.px,
          bottom: MediaQuery.of(context).padding.bottom,
          left: 14.px,
          right: 14.px,
        ),
        child: Column(
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,

                  children: [
                    Text(
                      '已选${controller.saleListChoose.length}',
                      style: TextStyle(
                        fontSize: 12.px,
                        color: AppColors.fontColor,
                      ),
                    ),
                    SizedBox(width: 8.px),
                    // Row(
                    //   children: [
                    //     Text(
                    //       '523',
                    //       style: TextStyle(
                    //         fontSize: 20.px,
                    //         color: AppColors.startColor,
                    //       ),
                    //     ),
                    //     Text(
                    //       '.00',
                    //       style: TextStyle(
                    //         fontSize: 14.px,
                    //         color: AppColors.startColor,
                    //       ),
                    //     ),
                    //   ],
                    // ),
                  ],
                ),
                GestureDetector(
                  onTap: () {
                    // showToast('查看动态逻辑');
                    controller.chooseAll();
                  },
                  child: Row(
                    children: [
                      Text(
                        '一键选中',
                        style: TextStyle(
                          fontSize: 13.px,
                          color: AppColors.startColor,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
            SizedBox(height: 15.px),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.center,

                  children: [
                    Text(
                      '总计',
                      style: TextStyle(
                        fontSize: 12.px,
                        color: AppColors.fontColor,
                      ),
                    ),
                    SizedBox(width: 8.px),
                    Row(
                      children: [
                        Text(
                          '${controller.totalPrice}',
                          style: TextStyle(
                            fontSize: 20.px,
                            color: AppColors.startColor,
                          ),
                        ),
                        // Text(
                        //   '.00',
                        //   style: TextStyle(
                        //     fontSize: 14.px,
                        //     color: AppColors.startColor,
                        //   ),
                        // ),
                      ],
                    ),
                  ],
                ),
                Row(
                  children: [
                    GestureDetector(
                      onTap: () {
                        controller.isBatch.value = false;
                        for (var element in controller.saleList) {
                          element.isSelect = false;
                          controller.totalPrice.value = '0';
                        }
                      },
                      child: Container(
                        width: 108.px,
                        height: 40.px,
                        decoration: BoxDecoration(
                          color: Color.fromRGBO(59, 62, 69, 1),
                          borderRadius: BorderRadius.circular(100),
                        ),
                        child: Center(
                          child: Text(
                            '取消',
                            style: TextStyle(
                              fontSize: 15.px,
                              color: AppColors.backgroundColorBlackE0,
                            ),
                          ),
                        ),
                      ),
                    ),
                    SizedBox(width: 10.px),
                    GestureDetector(
                      onTap: () {
                        if (controller.saleListChoose.isEmpty) {
                          showToast('请选择');
                          return;
                        }
                        controller.isSm().then((res) {
                          if (res == true) {
                            controller.createFirstOrder();
                          }
                        });

                        // Get.toNamed(
                        //   '${MarketCollectionBuyPage.path}?id=${1}',
                        //   arguments: {'id': '1'},
                        // );
                      },
                      child: Container(
                        width: 108.px,
                        height: 40.px,
                        decoration: BoxDecoration(
                          color: AppColors.startColor,
                          borderRadius: BorderRadius.circular(100),
                        ),
                        child: Center(
                          child: Text(
                            '确认购买',
                            style: TextStyle(
                              fontSize: 15.px,
                              color: AppColors.backgroundColorBlack,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
            SizedBox(height: 20.px),
          ],
        ),
      ),
    );
  }
}
